/*
 *  @TODO use DocumentFragment whenever possible
 */
if (typeof inweo.core.widget == "undefined") {
	inweo.core.widget = new Class({
		widget: null,
		__pack: function(parent) {
			try {
				this.widget.injectInside(parent);
			} catch(e) {
				inweo.error(e.message);
			}
		},
		registerWidget: function(ns) {
			this.ns = ns;
			this.id = this.newId(ns);
			this.widget.setProperty("id", this.id);
			ns = ns.toLowerCase() + "s";
			if (typeof window.inweo.widgets[ns] != "object") {
				window.inweo.widgets.push(ns);
				window.inweo.widgets[ns] = [];
			}
			window.inweo.widgets[ns].push(this.id);
			window.inweo.widgets[ns][this.id] = this;
		},
		remove: function() {
			//remove shawdow
			if (this.ns) {
				delete window.inweo.widgets[this.ns][this.id];
			}
			return this.widget.remove();
		},
		focus: function() {
			try {
				document.getElement(".widgetFocused").removeClass("widgetFocused");
			} catch(e) {}
			this.widget.addClass("widgetFocused");
			window.focus();
		},
		isFocused: function() {
			return this.widget.hasClass("widgetFocused");
		},
		hide: function() {
			return this.widget.setStyle("display", "none");
		},
		newId: function(namespace) {
			namespace = namespace || "ns";
			for (var i = 0;;i++) {
				if (document.getElementById(namespace + "_" + i) === null) {
					return (namespace + "_" + i).toString();
				}
			}
		},
		select: function(event) {
			if (event.control != true) {
				document.getElements(".widgetSelected").each(function(selected) {
					selected.removeClass("widgetSelected");
				});
			}
			this.widget.addClass("widgetSelected");
		},
		rounded: function(bool) {
			 if (bool == true) {
			 	var bTop = new Element("b");
				bTop.addClass("roundedTop");
				bTop.setHTML('<b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b>');
				bTop.injectTop(this.widget);

			 	var bBottom = new Element("b");
				bBottom.addClass("roundedBottom");
				bBottom.setHTML('<b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b>');
				bBottom.injectInside(this.widget);
			 }
		},
		shadow: function(bool) {
			// absolate position div with background "z-index: ((widget_z-index)-1);"
			if (bool == true) {
				if ($type(this.widgetShadow) == "element") {
					this.widgetShadow.setStyle("display", "block");
					return;
				}
				this.widget.addClass("widgetShadow");
				this.widgetShadowContainer = document.body.getElementById("widgetShadowContainer");
				if ($type(this.widgetShadowContainer) != "element") {
					this.widgetShadowContainer = new Element("div");
					this.widgetShadowContainer.setProperty("id", "widgetShadowContainer");
					this.widgetShadowContainer.injectInside(document.body);
				}
				this.widgetShadow = new Element("div");
				this.widgetShadow.addClass(this.id);
				//this.widgetShadow.setStyles(); not the S
				this.widgetShadow.setStyle("background-color", "black");
				this.widgetShadow.setStyle("position", "absolute");
				this.widgetShadow.setStyle("opacity", "0.1");
				this.widgetShadow.setStyle("left", this.widget.offsetLeft + 2);
				this.widgetShadow.setStyle("top", this.widget.offsetTop + 2);
				this.widgetShadow.setStyle("height", this.widget.offsetHeight);
				this.widgetShadow.setStyle("width", this.widget.offsetWidth);
				this.widgetShadow.injectInside(this.widgetShadowContainer);
				var zIndex = this.widget.getStyle("z-index");
				zIndex = (zIndex == isNaN) ? zIndex-- : "-1";
				this.widgetShadow.setStyle("z-index", zIndex);
			} else if (bool == false) {
				this.widget.removeClass("widgetShadow");
				this.widgetShadow.setStyle("display", "none");
			} else if (bool == null) {
				return this.widget.hasClass("widgetShadow");
			}
		},
		show: function() {
			return this.widget.setStyle("display", "block");
		},
		display: function(bool) {
			this.widget.scrollIntoView(bool);
		}
	});
}